<?php

namespace App\Models;
use Overtrue\LaravelLike\Traits\CanBeLiked;
use EloquentFilter\Filterable;
class Pictuer extends Model
{
    use CanBeLiked, Filterable, Traits\PictuerMd5Helper, Traits\PictuerAverageHelper;

    protected $fillable = ['user_id', 'view_counts', 'up_counts', 'color_average_id', 'title', 'md5', 'dhash', 'rgb', 'domain', 'path', 'filename', 'ext', 'width', 'height', 'size', 'hd', 'url', 'status', 'crawler_pictuer_id', 'article_id', 'category_id'];

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'pictuer_tag');
    }

    // 和 分类的关联
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function collect()
    {
      return $this->belongsToMany(Collect::class);
    }

    public function getTrueUrlAttribute()
    {
        if(!empty($this->domain))
        {
            return 'https://' . $this->domain . '/' . $this->path . '/' . $this->filename;
        }
        return config('app.url') . '/storage/uploads/' . $this->path . '/' . $this->filename;
    }
    public function color_average()
    {
      return $this->belongsTo(ColorAverage::class);
    }
}
